package com.enjoyor.soa.traffic.core.tsms.service.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.enjoyor.soa.traffic.core.tsms.domain.CommonRoute;
import com.enjoyor.soa.traffic.core.tsms.domain.ExecuteTaskInfo;
import com.enjoyor.soa.traffic.core.tsms.domain.RltRouteDevice;
import com.enjoyor.soa.traffic.core.tsms.domain.RltRoutePost;
import com.enjoyor.soa.traffic.core.tsms.domain.RltTaskTime;
import com.enjoyor.soa.traffic.core.tsms.domain.RouteInfo;
import com.enjoyor.soa.traffic.core.tsms.domain.RoutePoint;
import com.enjoyor.soa.traffic.core.tsms.domain.SchemeInfo;
import com.enjoyor.soa.traffic.core.tsms.domain.TaskDetailInfo;
import com.enjoyor.soa.traffic.core.tsms.domain.TaskInfo;
import com.enjoyor.soa.traffic.core.tsms.enums.EnumDicPublic;
import com.enjoyor.soa.traffic.core.tsms.mapper.ICommonRouteMapper;
import com.enjoyor.soa.traffic.core.tsms.mapper.IExecuteTaskInfoMapper;
import com.enjoyor.soa.traffic.core.tsms.mapper.IRltRouteDeviceMapper;
import com.enjoyor.soa.traffic.core.tsms.mapper.IRltRoutePostMapper;
import com.enjoyor.soa.traffic.core.tsms.mapper.IRltTaskTimeMapper;
import com.enjoyor.soa.traffic.core.tsms.mapper.IRouteInfoMapper;
import com.enjoyor.soa.traffic.core.tsms.mapper.IRoutePointMapper;
import com.enjoyor.soa.traffic.core.tsms.mapper.ISchemeInfoMapper;
import com.enjoyor.soa.traffic.core.tsms.mapper.ITaskDetailInfoMapper;
import com.enjoyor.soa.traffic.core.tsms.mapper.ITaskInfoMapper;
import com.enjoyor.soa.traffic.core.tsms.service.ITaskInfoService;
import com.enjoyor.soa.traffic.frame.logger.IPubLogMapper;
import com.enjoyor.soa.traffic.util.enums.EnumApplyStatus;
import com.enjoyor.soa.traffic.util.enums.log.LogInfoDic;
import com.enjoyor.soa.traffic.util.helper.GeomtryHelper;
import com.enjoyor.soa.traffic.util.helper.ReflectHelper;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;


@Service
@Transactional
@SuppressWarnings("rawtypes")
public class TaskInfoService implements ITaskInfoService {
	@Autowired
	private ISchemeInfoMapper schemeInfoMapper;
	
    @Autowired
    private ITaskInfoMapper taskInfoMapper;
    
    @Autowired
    private ITaskDetailInfoMapper taskDetailInfoMapper;
    
    @Autowired
    private IRltTaskTimeMapper rltTaskTimeMapper;

    @Autowired
    private IExecuteTaskInfoMapper executeTaskInfoMapper;
    
    @Autowired
    private ICommonRouteMapper commonRouteMapper;
    
    @Autowired
    private IRouteInfoMapper routeInfoMapper;
    
    @Autowired
    private IRoutePointMapper routePointMapper;
    
    @Autowired
    private IRltRouteDeviceMapper rltRouteDeviceMapper;
    
    @Autowired
    private IRltRoutePostMapper rltRoutePostMapper;
    
    @Autowired
    private IPubLogMapper pubLogMapper;
    
    @Override
    public TaskInfo selectTaskInfoById(String taskId) {
    	TaskInfo taskInfo = taskInfoMapper.selectById(taskId);
        return taskInfo;
    }
    
    @Override
	public List<TaskInfo> selectTaskInfoList(String taskId, String taskName, String schemeId, String taskRankId,
			String taskTypeId, String taskBeginTime, String taskEndTime, String startPlace, String targetPlace) {
		Map<String, Object> param = new HashMap<String, Object>();
    	param.put("taskId", taskId);
    	param.put("taskName", taskName);
    	param.put("schemeId", schemeId);
    	param.put("taskRankId", taskRankId);
    	param.put("taskTypeId", taskTypeId);
    	param.put("taskBeginTime", taskBeginTime);
    	param.put("taskEndTime", taskEndTime);
    	param.put("startPlace", startPlace);
    	param.put("targetPlace", targetPlace);
    	List<TaskInfo> taskInfoList = taskInfoMapper.selectTaskInfoList(param);
    	return taskInfoList;
    }
    
	@Override
	public Page selectTaskInfoPage(Page page, String taskId, String taskName, String schemeId, String taskRankId,
			String taskTypeId, String taskBeginTime, String taskEndTime, String startPlace, String targetPlace) {
		Map<String, String> mapBean = new HashMap<String, String>();
    	mapBean.put("taskId", taskId);
    	mapBean.put("taskName", taskName);
    	mapBean.put("schemeId", schemeId);
    	mapBean.put("taskRankId", taskRankId);
    	mapBean.put("taskTypeId", taskTypeId);
    	mapBean.put("taskBeginTime", taskBeginTime);
    	mapBean.put("taskEndTime", taskEndTime);
    	mapBean.put("startPlace", startPlace);
    	mapBean.put("targetPlace", targetPlace);
    	page.setMapBean(mapBean);
        return taskInfoMapper.selectPageInfo(page);
    }
    
    @Override
    public TaskDetailInfo selectExecuteTaskInfoById(String executeTaskId){
    	TaskDetailInfo taskDetailInfo = taskDetailInfoMapper.selectById(executeTaskId);
        return taskDetailInfo;
    }
    
    @Override
	public List<TaskDetailInfo> selectExecuteTaskBaseInfoList(String executeTaskId, String taskId, String taskName,
			String schemeId, String taskRankId, String taskTypeId, String beforeLicensePlate, String executeDate,
			String beginTime, String endTime, String startPlace, String targetPlace, String executeStatusId,
			String isWaitExecute) {
    	Map<String,Object> param = new HashMap<String ,Object>();
    	param.put("executeTaskId", executeTaskId);
    	param.put("taskId", taskId);
    	param.put("taskName", taskName);
    	param.put("schemeId", schemeId);
    	param.put("taskRankId", taskRankId);
    	param.put("taskTypeId", taskTypeId);
    	param.put("beforeLicensePlate", beforeLicensePlate);
    	param.put("executeDate", executeDate);
    	param.put("beginTime", beginTime);
    	param.put("endTime", endTime);
    	param.put("startPlace", startPlace);
    	param.put("targetPlace", targetPlace);
    	param.put("executeStatusId", executeStatusId);
    	param.put("isWaitExecute", isWaitExecute);
    	List<TaskDetailInfo> taskDetailInfo = taskDetailInfoMapper.selectExecuteTaskBaseInfoList(param);
    	return taskDetailInfo;
    }
    
    @Override
	public List<TaskDetailInfo> selectExecuteTaskDetailInfoList(String executeTaskId, String taskId, String taskName,
			String schemeId, String taskRankId, String taskTypeId, String beforeLicensePlate, String executeDate,
			String beginTime, String endTime, String startPlace, String targetPlace, String executeStatusId,
			String isWaitExecute) {
    	Map<String,Object> param = new HashMap<String ,Object>();
    	param.put("executeTaskId", executeTaskId);
    	param.put("taskId", taskId);
    	param.put("taskName", taskName);
    	param.put("schemeId", schemeId);
    	param.put("taskRankId", taskRankId);
    	param.put("taskTypeId", taskTypeId);
    	param.put("beforeLicensePlate", beforeLicensePlate);
    	param.put("executeDate", executeDate);
    	param.put("beginTime", beginTime);
    	param.put("endTime", endTime);
    	param.put("startPlace", startPlace);
    	param.put("targetPlace", targetPlace);
    	param.put("executeStatusId", executeStatusId);
    	param.put("isWaitExecute", isWaitExecute);
    	List<TaskDetailInfo> taskDetailList = taskDetailInfoMapper.selectExecuteTaskDetailInfoList(param);
    	
    	for(TaskDetailInfo taskDetail : taskDetailList){
			//查询线路Map；
			Map<String, Object> mapRoute = new HashMap<String, Object>();
			mapRoute.put("taskId", taskDetail.getTaskId());
			List<RouteInfo> routeInfoList = routeInfoMapper.getRouteInfoList(mapRoute);
			for(RouteInfo routeInfo : routeInfoList){
				String strGem = GeomtryHelper.getGeomToStringWithGson(routeInfo.getRouteGeo());
				routeInfo.setStrArea(strGem);
			}
			taskDetail.setRouteInfoList(routeInfoList);
		}
    	return taskDetailList;
    }
    
    @Override
	public Page selectExecuteTaskInfoPage(Page page, String executeTaskId, String taskId, String taskName,
			String schemeId, String taskRankId, String taskTypeId, String beforeLicensePlate, String executeDate,
			String beginTime, String endTime, String startPlace, String targetPlace, String executeStatusId,
			String isWaitExecute) {
    	Map<String,String> mapBean = new HashMap<String,String>();
    	mapBean.put("executeTaskId", executeTaskId);
    	mapBean.put("taskId", taskId);
    	mapBean.put("taskName", taskName);
    	mapBean.put("schemeId", schemeId);
    	mapBean.put("taskRankId", taskRankId);
    	mapBean.put("taskTypeId", taskTypeId);
    	mapBean.put("beforeLicensePlate", beforeLicensePlate);
    	mapBean.put("executeDate", executeDate);
    	mapBean.put("beginTime", beginTime);
    	mapBean.put("endTime", endTime);
    	mapBean.put("startPlace", startPlace);
    	mapBean.put("targetPlace", targetPlace);
    	mapBean.put("executeStatusId", executeStatusId);
    	mapBean.put("isWaitExecute", isWaitExecute);
    	page.setMapBean(mapBean);
        return taskDetailInfoMapper.selectPageInfo(page);
    }
    
    @Override
    public String insertTaskInfo(TaskInfo taskInfo, String userName) {
    	// 默认任务信息
        String taskId = "T" + TimeHelper.getUniqueString();
        taskInfo.setTaskId(taskId);
        taskInfo.setIsDelete(false);
        taskInfo.setTaskTypeId(EnumDicPublic.TYPE_COMMON.getKey());
        taskInfo.setTaskTypeName(EnumDicPublic.TYPE_COMMON.getValue());
        taskInfo.setCreateTime(TimeHelper.dateToLongString());
        taskInfo.setUpdateTime(TimeHelper.dateToLongString());
        taskInfoMapper.insert(taskInfo);
        //保存关联时间
        List<RltTaskTime> rltTimeList = taskInfo.getRltTimeList();
        for (RltTaskTime rltTime : rltTimeList) {
            String timeId = "TT" + TimeHelper.getUniqueString();
            rltTime.setTimeId(timeId);
            rltTime.setTaskId(taskId);
            rltTaskTimeMapper.insert(rltTime);
            
            //生成执行任务
            createExecuteTaskInfo(taskInfo, rltTime);
        }
        
        //生成关联线路信息:1为预案调用，2为历史任务，3为常用线路
        if(taskInfo.getRltInfoId() != null && taskInfo.getRltInfoId() != "") {
        	String rltInfoId = taskInfo.getRltInfoId();
        	if("1".equals(taskInfo.getRltSourceId())) {
            	//调用预案
            }else if("2".equals(taskInfo.getRltSourceId())) {
            	//历史任务
            	Map<String,Object> routeMap = new HashMap<String ,Object>();
            	routeMap.put("commonRouteId", rltInfoId);
            	List<RouteInfo> routeInfoList = routeInfoMapper.getRouteInfoList(routeMap);
            	for(RouteInfo routeInfo : routeInfoList){
            		String routeId = "R"+ TimeHelper.getUniqueString();
            		routeInfo.setRouteId(routeId);
            		routeInfo.setTaskId(taskId);
            		routeInfo.setIsDelete(false);
            		routeInfo.setCreateTime(TimeHelper.dateToLongString());
            		routeInfo.setUpdateTime(TimeHelper.dateToLongString());
            		routeInfoMapper.insert(routeInfo);
            		List<RoutePoint> routePointList = routeInfo.getRoutePointList();
            		for(RoutePoint routePoint : routePointList){
            			routePoint.setPointId("P"+ TimeHelper.getUniqueString());
            			routePoint.setTaskId(taskId);
            			routePoint.setRouteId(routeId);
            			routePointMapper.insert(routePoint);
            		}
            		List<RltRouteDevice> rltRouteDeviceList = routeInfo.getRltRouteDeviceList();
            		for(RltRouteDevice rltRouteDevice : rltRouteDeviceList){
            			rltRouteDevice.setDeviceId("RD"+ TimeHelper.getUniqueString());
            			rltRouteDevice.setTaskId(taskId);
            			rltRouteDevice.setRouteId(routeId);
            			rltRouteDeviceMapper.insert(rltRouteDevice);
            		}
            		List<RltRoutePost> rltRoutePostList = routeInfo.getRltRoutePostList();
            		for(RltRoutePost rltRoutePost : rltRoutePostList){
            			rltRoutePost.setPostId("RP"+ TimeHelper.getUniqueString());
            			rltRoutePost.setTaskId(taskId);
            			rltRoutePost.setRouteId(routeId);
            			rltRoutePostMapper.insert(rltRoutePost);
            		}
            	}
            }else if("3".equals(taskInfo.getRltSourceId())) {
            	//常用线路
            	CommonRoute commonRoute = commonRouteMapper.selectById(rltInfoId);
            	
            	RouteInfo routeInfo = commonToRouteInfo(commonRoute);
        		String routeId = "R"+ TimeHelper.getUniqueString();
        		routeInfo.setRouteId(routeId);
        		routeInfo.setTaskId(taskId);
        		routeInfo.setRouteTypeId(EnumDicPublic.ROUTE_EXECUTE.getKey());
        		routeInfo.setRouteTypeName(EnumDicPublic.ROUTE_EXECUTE.getValue());
        		routeInfo.setIsDelete(false);
        		routeInfo.setCreateTime(TimeHelper.dateToLongString());
        		routeInfo.setUpdateTime(TimeHelper.dateToLongString());
        		routeInfoMapper.insert(routeInfo);
        		List<RoutePoint> routePointList = routeInfo.getRoutePointList();
        		for(RoutePoint routePoint : routePointList){
        			routePoint.setPointId("P"+ TimeHelper.getUniqueString());
        			routePoint.setTaskId(taskId);
        			routePoint.setRouteId(routeId);
        			routePointMapper.insert(routePoint);
        		}
            }else {
            	return "false";
            }
        }
        //保存日志
        pubLogMapper.saveNoramlLogInfo(userName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
                String.format("添加警卫任务编号:{%s} 操作成功!", new Object[] { taskInfo.getTaskId() }),
                LogInfoDic.LOG_MODULAR_ENUM.TSMS_TASK_INFO_MANAGEMENT.getMessage());
        return taskId;
    }

    @Override
    public int editTaskInfo(TaskInfo taskInfo, String userName) {
    	taskInfo.setUpdateTime(TimeHelper.dateToLongString());
    	int resultNum = taskInfoMapper.updateByIdSelective(taskInfo);
    	//先删除，后保存新关联时间、生成执行任务
    	String taskId = taskInfo.getTaskId();
    	rltTaskTimeMapper.deleteByTaskId(taskId);
    	executeTaskInfoMapper.deleteByTaskId(taskId);
        List<RltTaskTime> rltTimeList = taskInfo.getRltTimeList();
        for (RltTaskTime rltTime : rltTimeList) {
            String timeId = "TT" + TimeHelper.getUniqueString();
            rltTime.setTimeId(timeId);
            rltTime.setTaskId(taskId);
            rltTaskTimeMapper.insert(rltTime);
            
            //生成执行任务
            createExecuteTaskInfo(taskInfo, rltTime);
        }
        //保存日志
        pubLogMapper.saveNoramlLogInfo(userName,null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode(),
                String.format("修改警卫任务编号:{%s} 操作成功!", new Object[] { taskInfo.getTaskId() }),
                LogInfoDic.LOG_MODULAR_ENUM.TSMS_TASK_INFO_MANAGEMENT.getMessage());
        return resultNum;
    }
    
    @Override
    public int editTaskBaseInfo(TaskInfo taskInfo, String userName) {
    	taskInfo.setUpdateTime(TimeHelper.dateToLongString());
    	int resultNum = taskInfoMapper.updateByIdSelective(taskInfo);
        //保存日志
        pubLogMapper.saveNoramlLogInfo(userName,null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode(),
                String.format("修改警卫任务编号:{%s} 操作成功!", new Object[] { taskInfo.getTaskId() }),
                LogInfoDic.LOG_MODULAR_ENUM.TSMS_TASK_INFO_MANAGEMENT.getMessage());
        return resultNum;
    }

    @Override
    public int deleteTaskInfo(String taskId, String userName) {
    	TaskInfo taskInfo = new TaskInfo();
    	taskInfo.setTaskId(taskId);
    	taskInfo.setIsDelete(true);
    	taskInfo.setUpdateTime(TimeHelper.dateToLongString());
    	int resultNum = taskInfoMapper.updateByIdSelective(taskInfo);
        //保存日志
        pubLogMapper.saveNoramlLogInfo(userName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_DELETE.getCode(),
                String.format("删除警卫任务编号:{%s} 操作成功!", new Object[] { taskId }),
                LogInfoDic.LOG_MODULAR_ENUM.TSMS_TASK_INFO_MANAGEMENT.getMessage());
        return resultNum;
    }
    
    @Override
    public String insertFastTaskInfo(TaskInfo taskInfo, String userName){
    	// 默认生成方案信息...
    	SchemeInfo schemeInfo = new SchemeInfo();
    	String schemeId = "S"+TimeHelper.getUniqueString();
		schemeInfo.setSchemeId(schemeId);
		schemeInfo.setSchemeName("(快)"+taskInfo.getTaskName());
		schemeInfo.setSchemeRankId(taskInfo.getTaskRankId());
		schemeInfo.setSchemeRankName(taskInfo.getTaskRankName());
		schemeInfo.setSchemeBeginDate(taskInfo.getTaskBeginDate().split(" ")[0]);
		schemeInfo.setSchemeEndDate(taskInfo.getTaskEndDate().split(" ")[0]);
		schemeInfo.setSchemeContent("--");
		schemeInfo.setSchemeMemo("--");
		schemeInfo.setIsDelete(false);
		schemeInfo.setCreateTime(TimeHelper.dateToLongString());
		schemeInfo.setUpdateTime(TimeHelper.dateToLongString());
		schemeInfo.setCreatePerson(userName);
		schemeInfo.setSchemeStatusId(EnumDicPublic.CHECKPASS.getKey());
		schemeInfo.setSchemeStatusName(EnumDicPublic.CHECKPASS.getValue());
		schemeInfoMapper.insert(schemeInfo);
    	
    	// 默认任务信息
        String taskId = "T" + TimeHelper.getUniqueString();
        taskInfo.setTaskId(taskId);
        taskInfo.setSchemeId(schemeId);
        taskInfo.setIsDelete(false);
        taskInfo.setTaskTypeId(EnumDicPublic.TYPE_FAST.getKey());
        taskInfo.setTaskTypeName(EnumDicPublic.TYPE_FAST.getValue());
        taskInfo.setCreateTime(TimeHelper.dateToLongString());
        taskInfo.setUpdateTime(TimeHelper.dateToLongString());
        taskInfo.setTaskClassId(EnumDicPublic.CLASS_ONCE.getKey());
        taskInfo.setTaskClassName(EnumDicPublic.CLASS_ONCE.getValue());
        taskInfo.setStartPlace("--");
        taskInfo.setTargetPlace("--");
        taskInfo.setTaskContent("--");
        taskInfoMapper.insert(taskInfo);
        //保存关联时间
        RltTaskTime rltTime = new RltTaskTime();
        String timeId = "TT" + TimeHelper.getUniqueString();
        rltTime.setTimeId(timeId);
        rltTime.setTaskId(taskId);
        rltTime.setBeginTime(taskInfo.getTaskBeginDate());
        rltTime.setEndTime(taskInfo.getTaskEndDate());
        rltTaskTimeMapper.insert(rltTime);
        //生成执行任务--相当于单次任务
        createExecuteTaskInfo(taskInfo, rltTime);
        //保存日志
        pubLogMapper.saveNoramlLogInfo(userName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
                String.format("添加快速特勤任务编号:{%s} 操作成功!", new Object[] { taskInfo.getTaskId() }),
                LogInfoDic.LOG_MODULAR_ENUM.TSMS_TASK_INFO_MANAGEMENT.getMessage());
        return taskId;
    }

    @Override
    public int editFastTaskInfo(TaskInfo taskInfo, String userName){
    	String taskId = taskInfo.getTaskId();
    	TaskInfo taskInfoSea = taskInfoMapper.selectById(taskId);
    	//修改方案信息...
    	SchemeInfo schemeInfo = new SchemeInfo();
    	String schemeId = taskInfoSea.getSchemeId();
		schemeInfo.setSchemeId(schemeId);
		schemeInfo.setSchemeName("(快)"+taskInfo.getTaskName());
		schemeInfo.setSchemeRankId(taskInfo.getTaskRankId());
		schemeInfo.setSchemeRankName(taskInfo.getTaskRankName());
		schemeInfo.setSchemeBeginDate(taskInfo.getTaskBeginDate());
		schemeInfo.setSchemeEndDate(taskInfo.getTaskEndDate());
		schemeInfo.setUpdateTime(TimeHelper.dateToLongString());
		schemeInfoMapper.updateByIdSelective(schemeInfo);
		//修改任务信息
        taskInfo.setSchemeId(schemeId);
    	taskInfo.setUpdateTime(TimeHelper.dateToLongString());
        taskInfo.setTaskClassId(EnumDicPublic.CLASS_ONCE.getKey());
        taskInfo.setTaskClassName(EnumDicPublic.CLASS_ONCE.getValue());
    	int resultNum = taskInfoMapper.updateByIdSelective(taskInfo);
    	//先删除，后保存新关联时间、生成执行任务
    	rltTaskTimeMapper.deleteByTaskId(taskId);
    	executeTaskInfoMapper.deleteByTaskId(taskId);
    	//保存关联时间
        RltTaskTime rltTime = new RltTaskTime();
        String timeId = "TT" + TimeHelper.getUniqueString();
        rltTime.setTimeId(timeId);
        rltTime.setTaskId(taskId);
        rltTime.setBeginTime(taskInfo.getTaskBeginDate());
        rltTime.setEndTime(taskInfo.getTaskEndDate());
        rltTaskTimeMapper.insert(rltTime);
        //生成执行任务--相当于单次任务
        createExecuteTaskInfo(taskInfo, rltTime);
        //保存日志
        pubLogMapper.saveNoramlLogInfo(userName,null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode(),
                String.format("修改快速特勤任务编号:{%s} 操作成功!", new Object[] { taskInfo.getTaskId() }),
                LogInfoDic.LOG_MODULAR_ENUM.TSMS_TASK_INFO_MANAGEMENT.getMessage());
        return resultNum;
    }

    @Override
    public int deleteFastTaskInfo(String taskId, String userName){
    	//删除方案信息...
    	TaskInfo taskInfoSea = taskInfoMapper.selectById(taskId);
    	if(taskInfoSea != null) {
        	SchemeInfo schemeInfo = new SchemeInfo();
        	String schemeId = taskInfoSea.getSchemeId();
    		schemeInfo.setSchemeId(schemeId);
    		schemeInfo.setIsDelete(true);
    		schemeInfo.setUpdateTime(TimeHelper.dateToLongString());
    		schemeInfoMapper.updateByIdSelective(schemeInfo);
    	}
    	//删除任务信息
    	TaskInfo taskInfo = new TaskInfo();
    	taskInfo.setTaskId(taskId);
    	taskInfo.setIsDelete(true);
    	taskInfo.setUpdateTime(TimeHelper.dateToLongString());
    	int resultNum = taskInfoMapper.updateByIdSelective(taskInfo);
        //保存日志
        pubLogMapper.saveNoramlLogInfo(userName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_DELETE.getCode(),
                String.format("删除快速特勤任务编号:{%s} 操作成功!", new Object[] { taskId }),
                LogInfoDic.LOG_MODULAR_ENUM.TSMS_TASK_INFO_MANAGEMENT.getMessage());
        return resultNum;
    }
    //生成执行任务---任务类别：单次TASKCLASS01/日常TASKCLASS02
    public void createExecuteTaskInfo(TaskInfo taskInfo, RltTaskTime rltTime) {
    	String taskId = taskInfo.getTaskId();
    	String taskClassId = taskInfo.getTaskClassId();
    	String taskPeriodId = taskInfo.getTaskPeriodId();
    	String schemeId = taskInfo.getSchemeId();
    	if(EnumDicPublic.CLASS_ONCE.getKey().equals(taskClassId)) {
        	String executeDate = rltTime.getBeginTime().split(" ")[0];
        	String executeBeginTime = rltTime.getBeginTime();
        	String executeEndTime = rltTime.getEndTime();
        	ExecuteTaskInfo executeTask = new ExecuteTaskInfo();
        	String executeTaskId = "TE" + TimeHelper.getUniqueString();
        	executeTask.setExecuteTaskId(executeTaskId);
        	executeTask.setTaskId(taskId);
        	executeTask.setSchemeId(schemeId);
        	executeTask.setExecuteDate(executeDate);
        	executeTask.setExecuteBeginTime(executeBeginTime);
        	executeTask.setExecuteEndTime(executeEndTime);
        	executeTask.setExecuteStatusId(EnumDicPublic.EXECUTE_WAIT.getKey());
        	executeTask.setExecuteStatusName(EnumDicPublic.EXECUTE_WAIT.getValue());
        	executeTask.setIsWaitExecute(true);
        	executeTaskInfoMapper.insert(executeTask);
        }else if(EnumDicPublic.CLASS_REPEAT.getKey().equals(taskClassId)) {
        	//根据任务周期拆分
        	if(EnumDicPublic.PERIOD_CUSTOM.getKey().equals(taskPeriodId)) {
        		String executeDate = rltTime.getBeginTime().split(" ")[0];
            	String executeBeginTime = rltTime.getBeginTime();
            	String executeEndTime = rltTime.getEndTime();
            	ExecuteTaskInfo executeTask = new ExecuteTaskInfo();
            	String executeTaskId = "TE" + TimeHelper.getUniqueString();
            	executeTask.setExecuteTaskId(executeTaskId);
            	executeTask.setTaskId(taskId);
            	executeTask.setSchemeId(schemeId);
            	executeTask.setExecuteDate(executeDate);
            	executeTask.setExecuteBeginTime(executeBeginTime);
            	executeTask.setExecuteEndTime(executeEndTime);
            	executeTask.setExecuteStatusId(EnumDicPublic.EXECUTE_WAIT.getKey());
            	executeTask.setExecuteStatusName(EnumDicPublic.EXECUTE_WAIT.getValue());
            	executeTask.setIsWaitExecute(true);
            	executeTaskInfoMapper.insert(executeTask);
        	}else {
        		int weekNum = -100;
        		if(EnumDicPublic.PERIOD_EVERYDAY.getKey().equals(taskPeriodId)) {
        			weekNum = -1;
        		}else if(EnumDicPublic.PERIOD_MONDAY.getKey().equals(taskPeriodId)) {
        			weekNum = 1;
        		}else if(EnumDicPublic.PERIOD_TUESDAY.getKey().equals(taskPeriodId)) {
        			weekNum = 2;
        		}else if(EnumDicPublic.PERIOD_WEDNESDAY.getKey().equals(taskPeriodId)) {
        			weekNum = 3;
        		}else if(EnumDicPublic.PERIOD_THURSDAY.getKey().equals(taskPeriodId)) {
        			weekNum = 4;
        		}else if(EnumDicPublic.PERIOD_FRIDAY.getKey().equals(taskPeriodId)) {
        			weekNum = 5;
        		}else if(EnumDicPublic.PERIOD_SATURDAY.getKey().equals(taskPeriodId)) {
        			weekNum = 6;
        		}else if(EnumDicPublic.PERIOD_SUNDAY.getKey().equals(taskPeriodId)) {
        			weekNum = 0;
        		}
        		try {
        			List<ExecuteTaskInfo> executeTaskInfoList = new ArrayList<ExecuteTaskInfo>();
					Date beginDate = TimeHelper.StringToDate(taskInfo.getTaskBeginDate(), "yyyy-MM-dd");
					Date endDate = TimeHelper.StringToDate(taskInfo.getTaskEndDate(), "yyyy-MM-dd");
					int days = (int) ((endDate.getTime() - beginDate.getTime()) / (1000*3600*24));
					for(int i=0; i < days+1; i++) {
						Date tempDate = TimeHelper.addDate(beginDate, i);
						if(weekNum == -1 || TimeHelper.dayOfWeek(tempDate) == weekNum) {
							String executeDate = TimeHelper.dateToShortString(tempDate);
							String executeBeginTime = rltTime.getBeginTime();
		                	String executeEndTime =  rltTime.getEndTime();
		                	if(executeBeginTime.length() < 10) {
								executeBeginTime = executeDate + " " + rltTime.getBeginTime();
			                	executeEndTime =  executeDate + " " + rltTime.getEndTime();
		                	}
		                	ExecuteTaskInfo executeTask = new ExecuteTaskInfo();
		                	String executeTaskId = "TE" + TimeHelper.getUniqueString();
		                	executeTask.setExecuteTaskId(executeTaskId);
		                	executeTask.setTaskId(taskId);
		                	executeTask.setSchemeId(schemeId);
		                	executeTask.setExecuteDate(executeDate);
		                	executeTask.setExecuteBeginTime(executeBeginTime);
		                	executeTask.setExecuteEndTime(executeEndTime);
		                	executeTask.setExecuteStatusId(EnumDicPublic.EXECUTE_WAIT.getKey());
		                	executeTask.setExecuteStatusName(EnumDicPublic.EXECUTE_WAIT.getValue());
		                	executeTask.setIsWaitExecute(true);
		                	executeTaskInfoList.add(executeTask);
						}
					}
					executeTaskInfoMapper.insertInBatch(executeTaskInfoList);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        	}
        }
    }
    //常用线路转化执行线路
    public RouteInfo commonToRouteInfo(CommonRoute commonRoute) {
    	RouteInfo routeInfo = new RouteInfo();
    	if(commonRoute != null) {
    		routeInfo.setRouteName(commonRoute.getCommonRouteName());
    		routeInfo.setRouteStartPlace(commonRoute.getRouteStartPlace());
    		routeInfo.setRouteTargetPlace(commonRoute.getRouteTargetPlace());
    		routeInfo.setRouteLength(commonRoute.getRouteLength());
    		routeInfo.setRoutePlanTime(commonRoute.getRoutePlanTime());
    		routeInfo.setRouteGeo(commonRoute.getRouteGeo());
    		routeInfo.setStrArea(commonRoute.getStrArea());
    		routeInfo.setRoutePointList(commonRoute.getRoutePointList());
    	}
    	return routeInfo;
    }

	@Override
	public List getTaskListByDateAndSchemeId(String schemeId, String date) {
		// TODO Auto-generated method stub
		List listAll = new ArrayList<>();
		Map<String, Object> map = new HashMap<>();
		map.put("beginDate", date);
		map.put("endDate", date);
		map.put("schemeId", schemeId);
		List<ExecuteTaskInfo> listExecuteTaskInfo = executeTaskInfoMapper.selectExecuteTaskList(map);
		for(int i = 0;i<listExecuteTaskInfo.size();i++){
			String taskId = listExecuteTaskInfo.get(i).getTaskId();
			Map<String, Object> routeMap = new HashMap<>();
			map.put("taskId", taskId);
			//根据任务编号获取关联线路集合；
			List<RouteInfo> listRoute = routeInfoMapper.getRouteInfoList(map); 
			//根据任务编号获取任务对象信息；
			TaskInfo taskInfo = taskInfoMapper.selectTaskInfoList(map).get(0);
			taskInfo.setRouteInfoList(listRoute);
			//创建任务关联执行任务集合；
			List<ExecuteTaskInfo> newListExecuteTaskInfo = new ArrayList<>();
			newListExecuteTaskInfo.add(listExecuteTaskInfo.get(i));
			taskInfo.setExecuteTaskList(newListExecuteTaskInfo);
			listAll.add(taskInfo);
		}
		return listAll;
	}
}
